﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using KMF.Common;
using KMF.DAL;
using KMF.WebUI.Areas.KMFadmin.Attributes;
using KMF.WebUI.Areas.KMFadmin.CustomHelpers;

namespace KMF.WebUI.Areas.KMFadmin.Controllers
{

    [AdminLoggedAuthorize]
    public class AdminGoldenTimeController : Controller
    {
        //
        // GET: /Admin/GoldenTime/

        public ActionResult Index()
        {
            var model = GoldenTimeCampainDb.GoldenTimeCampain_GetAdminList() ?? new List<GoldenTimeCampainDto>();
            return View(model);
        }

        public ActionResult Add()
        {
            var listStatus = FunctionHelper.GoldenTimeStatus();
            ViewBag.GoldenTimeStatus = listStatus;
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(GoldenTimeCampainDto model)
        {
            model.Id = Utility.Common.GenerateId();

            model.DateBegin = DateTime.Now;
            if (model.DateEnd <= DateTime.Now)
            {
                model.DateEnd = DateTime.Now;
            }
            model.CountProduct = 0;
            model.CountDate = 0;
            model.CountOrder = 0;
            model.TotalPrice = 0;
            model.CusWin = "";

            GoldenTimeCampainDb.Add(model);
            var listStatus = FunctionHelper.GoldenTimeStatus();
            ViewBag.GoldenTimeStatus = listStatus;
            return RedirectToAction("Index", "AdminGoldenTime");
        }

        public ActionResult GoldenTimeProductAdd(string goldId)
        {
            ViewBag.MenuList = FunctionHelper.GetMenuListThreeLevel() ?? new List<ProductCategoryDto>();
            ViewBag.GoldenTimeId = goldId;
            ViewBag.GoldenTimeStatus = FunctionHelper.GoldenTimeStatus();
            var _GoldenTimeCampainDto = GoldenTimeCampainDb.GetById(goldId);
            return View(_GoldenTimeCampainDto);
        }

        [HttpPost]
        public ActionResult GoldenTimeProductAdd(GoldenTimeCampainDto model)
        {
            if (model.DateEnd < DateTime.Now)
            {
                return RedirectToAction("GoldenTimeProductAdd", "AdminGoldenTime", new { goldId = model.Id });
            }
            GoldenTimeCampainDb.GoldenTimeCampain_UpdateStatus(model);
            return RedirectToAction("Index", "AdminGoldenTime");
        }

        public ActionResult GetListProductByGoldenTime(string goldId)
        {
            var model = GoldenTimeProductDb.GetAllGoldenTimeProductByGoldenTimeId(goldId);
            if (model == null || model.Count <= 0)
                return View();
            return PartialView(model);
        }

        public JsonResult GetProductByCate(string cateId)
        {
            var totalPage = 0;
            //lay du lieu theo cate
            var result = ProductDb.GetAllProductByCategoryId(1, 1000, ref totalPage, cateId) ?? new List<ProductAllPagingDto>();
            return Json(result, JsonRequestBehavior.AllowGet);
        }

       
        public JsonResult GetProductFromGrid(string ListId, string goldenTimeId)
        {
            //xoa cac ban ghi da them truoc do de luu lai
            //GoldenTimeProductDb.DelAllProductByGoldenTimeId(goldenTimeId);
            //luu cac san pham duoc chon
            var listId = ListId.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (listId.Length > 0)
            {
                foreach (var s in listId)
                {
                    ////kiem tra xem san pham nay da co hay chua
                    //var chkExist = GoldenTimeProductDb.GoldenTimeProduct_SelectByCampainAndProductId(goldenTimeId, s);
                    var _GoldenTimeProductDto = new GoldenTimeProductDto()
                    {
                        Id = Utility.Common.GenerateId(),
                        ProductId = s,
                        CampaintId = goldenTimeId,
                        ViewTime = 1,
                        Bought = false,
                        Active = true,
                        PromotionUnit = 0,
                        PriceUnit = 0
                    };
                    GoldenTimeProductDb.GoldenTimeProduct_AddToGrid(_GoldenTimeProductDto);
                }
            }
            //lay du lieu theo cate
            var result = GoldenTimeProductDb.GetAllGoldenTimeProductByGoldenTimeId(goldenTimeId) ?? new List<GoldenTimeProductTopListDto>();
            //cap nhap lai so luong cho GoldeTimeCampain
            var goldenTimeCampainDto = new GoldenTimeCampainDto()
                                            {
                                                Id = goldenTimeId,
                                                CountProduct = result.Count
                                            };
            GoldenTimeCampainDb.GoldenTimeCampain_UpdateCountProduct(goldenTimeCampainDto);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DelAllProductFromGrid(string goldenTimeId)
        {
            // Delete record of product
            var result = GoldenTimeProductDb.DelAllProductByGoldenTimeId(goldenTimeId);
            return Json(result > 0, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DelGoldenTimeProductById(string ProductId, string goldenTimeId)
        {
            // Delete record of product
            var result = GoldenTimeProductDb.DelGoldenTimeProductById(ProductId, goldenTimeId);
            return Json(result > 0, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GoldenTimeProductAddPersent(string GoldenProductId, int per, string priceOld)
        {
            var result = "";
            if (per >= 0)
            {
                decimal priceNew = 0;
                if(per==100)
                {
                    result = "0";
                    priceNew = 0;
                }
                else if (per == 0)
                {
                    priceNew = Convert.ToDecimal(priceOld);
                    result = priceNew.ToString("##,###,###");
                }
                else
                {
                    priceNew = Convert.ToDecimal(priceOld) - ((Convert.ToDecimal(priceOld) / 100) * per);
                    result = priceNew.ToString("##,###,###");
                }

                GoldenTimeProductDb.UpdatePromotionUnit(GoldenProductId, per, priceNew);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

    }
}
